﻿using UnityEngine;

namespace SimWar.Scripts.Components.CameraGesture
{
    /// <summary>
    /// 鼠标控制器
    /// </summary>
    public class MouseProvider : IInputProvider
    {
        /// 鼠标枚举
        enum MouseTypeEnum
        {
            LEFT = 0
        }

        /// <summary>
        /// 缩放距离
        /// </summary>
        private float scrollDistance;
        Vector3 lastPointerPosition;

        public MouseProvider(IInput input) : base(input)
        {
        }

        public override Vector2 PointerPosition
        {
            get
            {
                return Input.mousePosition;
            }
        }
        public override void ProcessPointer()
        {
            // 按下鼠标、轴
            if (Input.GetMouseButtonDown((int)MouseTypeEnum.LEFT))
            {
                // 触发开始回调函数
                mInput.OnPointerDown(Input.mousePosition);
                lastPointerPosition = Input.mousePosition;
            }
            // 松开鼠标、轴
            else if (Input.GetMouseButtonUp((int)MouseTypeEnum.LEFT))
            {
                // 触发结束回调函数
                mInput.OnPointerUp(Input.mousePosition);
            }
            // 如果鼠标在按住状态
            else if (Input.GetMouseButton((int)MouseTypeEnum.LEFT))
            {
                // 触发移动回调函数
                mInput.OnPointerMove(Input.mousePosition, Input.mousePosition - lastPointerPosition);
                lastPointerPosition = Input.mousePosition;
            }
            else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
            {// 触发移动回调函数
                Vector2 v = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
                mInput.OnPointerMove(Input.mousePosition, v);
                lastPointerPosition = v;
            }
            // 鼠标滚轮拉近拉远
            this.scrollDistance = Input.GetAxis("Mouse ScrollWheel");
            // 触发缩放回调函数
            if (this.scrollDistance != 0)
                mInput.OnPointerZoom(Input.mousePosition, this.scrollDistance * 1000);
        }
    }
}